package com.example.unique.ui.person

import android.annotation.SuppressLint
import android.database.Cursor
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.unique.database.DatabaseHelper
data class User(
    val id: Int,
    var academy: String,
    var club: String,
    var name: String,
    val role: Int,
    val sno: String,
    var password: String
)
class PersonViewModel(private val databaseHelper: DatabaseHelper) : ViewModel() {
    // LiveData 用于更新 UI
    private val _userData = MutableLiveData<User?>()
    val userData: LiveData<User?> = _userData

    private val _avatarUri = MutableLiveData<String?>()
    val avatarUri: LiveData<String?> = _avatarUri
    // 通过学号获取用户信息
    @SuppressLint("Range")
    fun getUserBySno(sno: String) {
        val cursor: Cursor = databaseHelper.getUserBySno(sno)

        // 如果查询到数据
        if (cursor.moveToFirst()) {
            val user = User(
                id = cursor.getInt(cursor.getColumnIndex("id")),
                academy = cursor.getString(cursor.getColumnIndex("academy")),
                club = cursor.getString(cursor.getColumnIndex("club")),
                name = cursor.getString(cursor.getColumnIndex("name")),
                role = cursor.getInt(cursor.getColumnIndex("role")),
                sno = cursor.getString(cursor.getColumnIndex("sno")),
                password = cursor.getString(cursor.getColumnIndex("password"))
            )
            _userData.value = user // 更新 LiveData
        } else {
            _userData.value = null // 如果没有查询到，设置为 null
        }

        cursor.close()
    }

    // 修改头像路径
    fun setAvatarUri(uri: String) {
        _avatarUri.value = uri
    }
}
